import 'package:flutter/material.dart';
import 'package:freedom_flutter_mall/values/router_values.dart';

class RouterUtils {
  static bool canPop = true;

  static dynamic arguments;

  static getArgument() {
    var data = arguments;
    arguments = null;
    return data;
  }

  static Future<dynamic> toRouter(BuildContext context, String router, { dynamic arguments,}) async {
    canPop = true;
    RouterUtils.arguments = arguments;
    var result = await Navigator.pushNamed(context, router, arguments: arguments);
    return result;
  }

  static Future<dynamic> backToRouter(BuildContext context, String router, {dynamic arguments,}) async {
    canPop = true;
    RouterUtils.arguments = arguments;
    var result = await Navigator.popAndPushNamed(context, router, arguments: arguments);
    return result;
  }

  static Future<dynamic> toPage(BuildContext context, Widget page,) async {
    canPop = true;
    var result = await Navigator.push(context, MaterialPageRoute(builder: (context) => page));
    return result;
  }

  static Future toRouterCloseAll(BuildContext context, String router,) async {
    canPop = false;
    var result = await Navigator.pushNamedAndRemoveUntil(context, router, ModalRoute.withName(router));
    return result;
  }

  static back(BuildContext context, {dynamic arguments,
  }) {
    if (canPop) {
      Navigator.pop(context, arguments);
    } else {
      toRouterCloseAll(context, RouterValues.mainTab);
    }
  }

}
